/*企业号管理*/
define(['app', 'pagination', 'datetime'], function(appCRM) {
    'use strict';

     appCRM.controller('Qy', function($scope, $rootScope, dataFactoryCRM, $q, $timeout, crmConst) {
        $scope.qyListData = null;
        $scope.search_param = {name:''};
        $scope.detailData = {};
        $scope.qyPage = { pageAll:1, pageNow:1, pageSize:$scope.psize, k: 3022 };


        $scope.viewQyDetail = function(qyId){
            $scope.detailData = {};
             dataFactoryCRM.getDataByAPI('qy/detail?code='+qyId )
                .then(function(d){
                    if (d&&d.rcode) {
                        $timeout(function(){
                            $scope.detailData = d.data;
                        });
                        $('.ui.detail').modal({
                            autofocus:false,
                            closable:false,
                            onApprove:function(){
                                return true;
                            },
                        }).modal('show');
                    }
                    else{
                        $scope.api_error( d.msg );
                    }
                },
                function(){
                    $scope.api_error('接口请求失败');
                }
            );
        };

        $scope.viewQyApp = function(qyId){
            $rootScope.go('/qyapp/'+qyId);
        };

         $scope.api_error = function ( msg ) {
            $('.modal.modal2').remove();
            $('<div class="ui small modal modal2" style="text-align: center;"><div class="header">提示</div><div class="content"><i class="huge red warning outline icon"></i>' +
                '<p style="padding-top: 15px;">'+msg+'</p>' +'</div></div>'
            ).modal('show');
        };

        $scope.search = function(){
            $scope.qyPage.pageNow = 1;
            $scope.getDataList();
        };

        $scope.enterSearch = function( evt ){
            if (evt && evt.keyCode==13) {
                 $scope.search();
            }
        };

        $scope.getDataList = function(){
             var listParam = $scope.search_param;
            listParam['psize'] = $scope.qyPage.pageSize;
            listParam['p'] = $scope.qyPage.pageNow;
            var _url = 'qy/index?' + $.param( listParam );
            dataFactoryCRM.getDataByAPI(_url, true).then(function(data) {
                if (data.rcode == 1) {
                    $scope.qyListData = data.data;
                    if ( data.page > 1) {
                        $scope.qyPage.pageSize = data.psize;
                        $scope.qyPage.pageAll = data.page;
                        $scope.qyPage.pageNow = data.p;
                    } else {
                        $scope.qyPage.pageSize = $scope.psize;
                        $scope.qyPage.pageAll = 1;
                        $scope.qyPage.pageNow = 1;
                    }
                }
            });
        };

         //初始化页面显示数据（如果是新弹出独立窗口则从父窗口获取）
        window.loadClientCacheToScope($scope, function(){
            $scope.getDataList();
        });

     });
});